import { Action, combineReducers, configureStore, ThunkAction } from '@reduxjs/toolkit'
import { persistReducer } from 'redux-persist'
import storage from 'redux-persist/es/storage'
import global from './global'

const reducers = combineReducers({
  global,
})
//这是持久化的配置
const persistConfig = {
  key: 'root',
  storage,
  //持久化白名单(以combineReducers的属性名为标准，只能精确到仓库，不能像pinia一样精确到仓库的属性)
  whiteList: ['global'],
}
//封装持久化
const persistedReducer = persistReducer(persistConfig, reducers)
//此时的reducer就是持久化后的配置
export const store = configureStore({
  reducer: persistedReducer,
  middleware: getDefaultMiddleware =>
    getDefaultMiddleware({
      serializableCheck: false,
    }),
})

export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch
export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, RootState, unknown, Action<string>>
